//
//  WJEssAndNewModel.m
//  WJBSApp
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJEssAndNewModel.h"
#import "WJTagsModel.h"
// 乱七八糟的Model
#import "WJUModel.h"
#import "WJTopModel.h"
#import "WJVideoModel.h"
#import "WJImageModel.h"
#import "WJAudioModel.h"
#import "WJHtmlModel.h"
#import "WJGifModel.h"

@implementation WJEssAndNewModel



-(void)dealloc
{
    [_id release];
    [_u release];
    [_uModel release];
    [_passtime release];
    [_text release];
    
    [_type release];
    [_gif release];
    [_gifModel release];
    [_video release];
    [_videoModel release];
    [_image release];
    [_imageModel release];
    [_audio release];
    [_audioModel release];
    [_html release];
    [_htmlModel release];
    
    [_top_comment release];
    [_topModel release];
    [_tags release];
    
    [_Mecomment release];
    [_Medown release];
    [_Meforward release];
    [_Meup release];
    
    [_share_url release];
    [_bookmark release];
    [super dealloc];
    
}

- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super initWithDic:dic];
    if (self) {
        
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSDictionary *tempDic in _tags) {
            
            WJTagsModel *model = [[WJTagsModel alloc] initWithDic:tempDic];
            [arr addObject:model];
            [model release];
        }
        
        self.tags = [NSArray arrayWithArray:arr];
        self.topModel = [[WJTopModel alloc]initWithDic:self.top_comment];
        self.topModel.topUModel = [[WJUModel alloc]initWithDic:self.topModel.u];
        self.uModel = [[WJUModel alloc]initWithDic:self.u];
        self.videoModel = [[WJVideoModel alloc]initWithDic:self.video];
        self.audioModel = [[WJAudioModel alloc]initWithDic:self.audio];
        self.imageModel = [[WJImageModel alloc]initWithDic:self.image];
        self.htmlModel = [[WJHtmlModel alloc]initWithDic:self.html];
        self.gifModel = [[WJGifModel alloc]initWithDic:self.gif];
        
    }
    return self;

}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"comment"]) {
        
        self.Mecomment = [NSString stringWithFormat:@"%@",value];
    }
    if ([key isEqualToString:@"up"]) {
        
        self.Meup = [NSString stringWithFormat:@"%@",value];
    }
    if ([key isEqualToString:@"down"]) {
        
        self.Medown = [NSString stringWithFormat:@"%@",value];
    }
    if ([key isEqualToString:@"forward"]) {
        
        self.Meforward = [NSString stringWithFormat:@"%@",value];
    }
}



- (CGFloat)cellHeight
{
    
    if (!_cellHeight) {
        
        /******** 段子的cell ********/
        
        //计算文字的高度
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [self.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        
        //cell的高度
        //段子的高度
        _cellHeight = 35 + rect.size.height;
        
        /******** 图片的cell ********/
        
        if ([self.type isEqualToString:@"image"]) {//图片帖子
            
            NSInteger newHeight = self.imageModel.height * (WIDTH - 20) / self.imageModel.width;
            
            if (newHeight > HEIGHT) {
                
                _cellHeight =  _cellHeight + 5 + HEIGHT / 2;
                
            }else{
                
                _cellHeight = _cellHeight + 5 + newHeight;
                
            }
            
        }else if ([self.type isEqualToString:@"html"]){
            
            _cellHeight = _cellHeight + 5 + HEIGHT / 4;
            
        } else if ([self.type isEqualToString:@"gif"]) {//图片帖子
            
            NSInteger newHeight = self.gifModel.height * (WIDTH - 20) / self.gifModel.width;
            
            _cellHeight = _cellHeight + 5 + newHeight;
            
        }else if ([self.type isEqualToString:@"video"]){
            
            NSInteger newHeight = self.videoModel.height * (WIDTH - 20) / self.videoModel.width;
            
            _cellHeight = _cellHeight + 5 + newHeight;
            
        }else if ([self.type isEqualToString:@"audio"]){
            
            NSInteger newHeight = self.audioModel.height * (WIDTH - 20) / self.audioModel.width;
            
            _cellHeight = _cellHeight + 5 + newHeight;
            
        }
        
        //最热评论的尺寸，如果有评论
        if (self.top_comment != nil ) {
            //给评论内容赋值
            NSString *str = [NSString stringWithFormat:@"%@:%@",self.topModel.topUModel.name ,self.topModel.content];
            NSDictionary *discussdic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
            CGRect discussRect = [str boundingRectWithSize:CGSizeMake(WIDTH - 20, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:discussdic context:nil];
            
            _cellHeight = _cellHeight + 5 + discussRect.size.height + 10;
        }        
        // 下面还有标签 和 按钮        间距 长度 按钮长度 多留的间距(我想还是用来加一个Lable吧)
        _cellHeight =  _cellHeight + 10 + 20 + 30 + 20;
    }
    
    return _cellHeight;

}






@end
























